/**
 * 
 */
package com.ekingstar.framework.action;

import java.util.List;

import org.apache.struts2.config.Action;
import org.apache.struts2.config.Result;
import org.apache.struts2.dispatcher.ServletActionRedirectResult;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.conversion.annotations.Conversion;
import com.opensymphony.xwork2.util.Element;
import com.ekingstar.framework.app.ApplicationParameter;
import com.ekingstar.framework.domain.Role;
import com.ekingstar.framework.domain.User;
import com.ekingstar.framework.service.IRoleService;
import com.ekingstar.framework.service.IUserRoleService;
import com.ekingstar.framework.service.IUserService;
import com.ekingstar.framework.util.Page;

/**
 * 
 *
 */

@SuppressWarnings("serial")
@Action(name="user",namespace="/user")
@Result(name="success",value="user!listUserPage.action",type=ServletActionRedirectResult.class)	
//@Result(name="success",value="user",type=ActionChainResult.class,params={"actionName","user","namespace","/user","methodName","listUserPage"})			
@Conversion
public class UserAction extends ActionSupport{
	
	
	private IUserService userService;
	private IRoleService roleService;
	private IUserRoleService userRoleService;
	private User user;
	private List<User> users;
	@Element(Role.class)
	private List<Role> roles;
	private List hasroles;
	private List canUse;
	private List canGrant;
	private Page page;
	private int pageNo = 1;
	private int pageSize = ApplicationParameter.INSTANCE.getPageSize();
	
	
	private String id;
	
	public String execute(){
		this.page = userService.getUserlist(user,pageNo, pageSize);
		return "list";
	}
	public String listUserPage(){
		this.page = userService.getUserlist(user,pageNo, pageSize);
		return "list";
	}
	/*public String listQueryUserPage(){
		this.page = userService.getUserlistByUser(user,pageNo, pageSize);
		return "list";
	}*/
	public String viewUser(){
		this.user = userService.viewUser(id);
		this.hasroles = roleService.getRoleByUserId(this.user.getId());
		return "view";
	}
	
	public String deleteUser(){
		if (user == null || user.getId() == null || user.getId().trim().equals("")) return "success";
		userService.deleteUser(user.getId());
		return "success";
	}
	
	public String addRole(){
		this.user = userService.viewUser(id);
		this.roles = roleService.getAllRoles();
		this.hasroles = roleService.getRoleByUserId(this.user.getId());
		if (roles != null && hasroles != null){
			for (int i = 0; i < roles.size();i++){
				Role r = roles.get(i);
				int index = hasroles.indexOf(r);
				System.out.println(index);
				if (index >= 0){
					Role hasRole = (Role)hasroles.get(index);
					r.setCanGrant(hasRole.getCanGrant());
					r.setCanUse(hasRole.getCanUse());
				}
			}
		}
		return "addrole";
	}
	
	public String valueUserType(String val){
		return User.Type.valueOf(val).toString();
	}
	public String addRoleSub(){
		if(this.roles == null || this.roles.equals("")){
			return "addrole";
		}
		if(userRoleService.userAddRole(this.user.getId(),this.roles)){
			return "success";
		}else{
			this.addFieldError("error","当前用户获得失败(匿名无权限操作)");
			return "addrole";
		}
	}
	
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}
	
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public IUserService getUserService() {
		return userService;
	}
	
	public String tree(){
		
		return "tree";
	}
	public List<Role> getRoles() {
		return roles;
	}
	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}
	
	public List getHasroles() {
		return hasroles;
	}
	public void setHasroles(List hasroles) {
		this.hasroles = hasroles;
	}
	public List getCanUse() {
		return canUse;
	}
	public void setCanUse(List canUse) {
		this.canUse = canUse;
	}
	public List getCanGrant() {
		return canGrant;
	}
	public void setCanGrant(List canGrant) {
		this.canGrant = canGrant;
	}
	public IRoleService getRoleService() {
		return roleService;
	}
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}
	public IUserRoleService getUserRoleService() {
		return userRoleService;
	}
	public void setUserRoleService(IUserRoleService userRoleService) {
		this.userRoleService = userRoleService;
	}

}
